package com.ecity.ui {
	import com.ecity.events.MenuEvent;
	import com.ecity.resource.Resource;
	import com.greensock.core.PropTween;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Yicheng.Zhang
	 */
	public class SubMenu extends Menu {
		[Embed(source = "/assets/sub03.png")] private var SubMenu03:Class;
		[Embed(source = "/assets/subBoder.png")] private var SubBorder:Class;
		private var menulist:Array = [["剧本角色","上传剧本"],["剧本角色","上传剧本","剧本投票","剧本拍摄"],["剧本角色"],["剧本角色","剧本投票","剧本拍摄"]];
		private var bmptitle:Bitmap; 
		private var hborder:Bitmap;
		private var menuBed:Sprite;
		private var bgBed:Sprite;
		public var index:int;
	//	
		public var isSelected:Boolean = false;
		public var numMenu:int = 0;
		
		private var title_n:Bitmap;
		private var title_u:Bitmap;
		private var bitmap_title:Bitmap;
		
		public function SubMenu(index:int) {
			title_u = Resource.bitmapUIs.get("st_ysjd0") as Bitmap;
			title_n = Resource.bitmapUIs.get("st_ysjd1") as Bitmap;
			this.index = index;
			menuBed = new Sprite();
			backgroundDraw();
			addListener();
			this.addChild(menuBed);
			bitmap_title = new Bitmap(title_n.bitmapData);
			hborder =new SubBorder();
			bgBed.addChild(hborder);
			hborder.alpha = 0;
			this.menuHeight = 38;
			menuBed.y = menuHeight;
			this.drawBackground();
			this.addBmpText(bitmap_title, 140, 10);
			addChildMenus();
		}
		
		private function addListener():void {
			bgBed.addEventListener(MouseEvent.MOUSE_OVER, onHoverHandler);
			bgBed.addEventListener(MouseEvent.MOUSE_OUT, onHoverOut);
			bgBed.addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		private function switchBitmapData(status:int):void {
			bitmap_title.bitmapData = status==0?title_n.bitmapData:title_u.bitmapData;
		}
		
		protected function backgroundDraw():void {
			bgBed = new Sprite();
			this.addChild(bgBed);
			this.bgColors = [0x0d1b3c, 0x152f71];
			this.alphas= [100, 100];
			bgBed.graphics.beginGradientFill(GradientType.LINEAR, bgColors, alphas, ratios,matrix);
			bgBed.graphics.drawRect(0, 0, menuWidth, menuHeight);
			bgBed.graphics.endFill();
		}
		/**
		 * tianjia 
		 */
		protected function addChildMenus():void {
			numMenu = menulist[index].length;
			for (var i:int = 0; i < numMenu;  i++) {
				var linker:LinkMenu = new LinkMenu(menulist[index][i], i);
				linker.y = linker.menuIndex* -27;
				linker.alpha = 0;
				linker.addEventListener(MenuEvent.LINK_CHANGED, onLinkerVisited);
				menuBed.addChild(linker);
			}
		}
		
		private function onLinkerVisited(e:MenuEvent):void {
			var linker:LinkMenu = e.target as LinkMenu;
			for (var i:int = 0; i < numMenu; i++) {
				if (i != linker.menuIndex) {
					var othersLinker:LinkMenu= menuBed.getChildAt(i)  as LinkMenu;;
					othersLinker.backOriginal();
				}
			}
			
		}
		/**
		 * 展开
		 */
		protected function expand():void {
			if (isExpland) return;
			isExpland = true;
			isSelected = true;
			for (var i:int = 0; i < menuBed.numChildren; i++) {
				var linker:LinkMenu = menuBed.getChildAt(i) as LinkMenu;
				TweenLite.to(bitmap_title, 0.5, {x:180});
				TweenLite.to(linker, 0.5, {delay:(menuBed.numChildren-linker.menuIndex)* 0.2+1, y:0,alpha:1});
			}
		}
		
		/**
		 * 收起
		 */
		public function pickup(stepdelay:Number=0.1):void {
			if (!isExpland) return;
			isExpland = false;
			isSelected = false;
			TweenLite.to(hborder, 0.2, {alpha:0});
			for (var i:int = 0; i < menuBed.numChildren; i++) {
				var linker:LinkMenu = menuBed.getChildAt(i) as LinkMenu;
				linker.backOriginal();
				TweenLite.to(bitmap_title, 0.5, {x:140});
				TweenLite.to(linker, 0.5, {delay:linker.menuIndex * stepdelay, y:linker.menuIndex * -27 ,alpha:0});
			}
		}

		
		private function onHoverHandler(e:MouseEvent):void {
			if (!isSelected) {
				TweenLite.to(hborder, 0.2, { alpha:0.3 } );
				switchBitmapData(1);
			}
		}
		
		private function onHoverOut(e:MouseEvent):void {
			if (!isSelected) {
				TweenLite.to(hborder, 0.2, { alpha:0 } );
				switchBitmapData(0);
			}
		}
		
		private function onClickHandler(e:MouseEvent):void {
			if (!isSelected) {
				switchBitmapData(0);
				this.dispatchEvent(new MenuEvent(MenuEvent.MENU_CHANGED));
				TweenLite.to(hborder, 0.2, { alpha:0.8 } );
				expand();
				isSelected = true;
			}
		}
		

		

		
	}

}